﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Modelo_ETdA.Negocio.Analise.Relatorio;
using Modelo_ETdA.Negocio.Analise.Avaliacao;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.fonts.cmaps;

namespace Modelo_ETdA.Dados
{
    public class DadosRelatorio
    {
        /* 
         * Definição das Fonts
         */
        private static Font F_TITULO = new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.NORMAL);
        private static Font F_SUBTITULO = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
        private static Font F_NORMAL = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
        private static Font F_SMALLER_NORMAL = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
        private static Font F_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
        private static Font F_SMALLER_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
        private static Font F_MINI_ITALIC = new Font(Font.FontFamily.TIMES_ROMAN, 6, Font.ITALIC);

        private static string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ETdA\\";


        public DadosRelatorio()
        {
        }

        private float criaPontosIdades(ref List<int> y, List<int> idades)
        {
            Boolean enc;
            int sum = 0;

            foreach (int id in idades)
            {
                enc = false;
                if ((id <= 15) && (!enc)) { y[0] = y[0] + 1; enc = true; }
                if ((id <= 20) && (!enc)) { y[1] = y[1] + 1; enc = true; }
                if ((id <= 25) && (!enc)) { y[2] = y[2] + 1; enc = true; }
                if ((id <= 30) && (!enc)) { y[3] = y[3] + 1; enc = true; }
                if ((id <= 35) && (!enc)) { y[4] = y[4] + 1; enc = true; }
                if ((id <= 40) && (!enc)) { y[5] = y[5] + 1; enc = true; }
                if ((id <= 45) && (!enc)) { y[6] = y[6] + 1; enc = true; }
                if ((id <= 50) && (!enc)) { y[7] = y[7] + 1; enc = true; }
                if ((id <= 55) && (!enc)) { y[8] = y[8] + 1; enc = true; }
                if ((id <= 60) && (!enc)) { y[9] = y[9] + 1; enc = true; }
                if ((id <= 70) && (!enc)) { y[10] = y[10] + 1; enc = true; }
                if ((id <= 80) && (!enc)) { y[11] = y[11] + 1; enc = true; }
                if (!enc) { y[12] = y[12] + 1; }

                sum += id;
            }
            float ret = (float)sum / (float)idades.Count;
            return ret;
        }

        private BaseColor getColor(int cod)
        {
            if (cod == 0) return BaseColor.RED;
            if (cod == 1) return BaseColor.YELLOW;
            if (cod == 2) return BaseColor.GREEN;
            return BaseColor.LIGHT_GRAY;
        }


        public int imprimirPrioridadeGeral(RelatorioPrioridadesGeral rel)
        {
            DateTime now = DateTime.Today;

            String filename = path + "Rel_PrioridadeGeral_" + rel.AmbComercial.Cabeçalho.Nome + "_" + now.Day + now.Month + now.Year + ".pdf";
            Boolean primo;

            iTextSharp.text.Document oDoc = new iTextSharp.text.Document();

            try
            {
                PdfWriter writer = PdfWriter.GetInstance(oDoc, new FileStream(filename, FileMode.Create));

                oDoc.Open();

                PdfContentByte direct_c = writer.DirectContent;

                Paragraph p = new Paragraph();
                p.Alignment = Element.ALIGN_CENTER;
                p.Add(new Phrase("Relatório de Prioridades Geral\n", F_TITULO));
                string s_cod = "COD: " + rel.AmbComercial.Cabeçalho.Cod;
                p.Add(new Phrase(s_cod, F_SUBTITULO));
                oDoc.Add(p);

                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nCódigo Analistas:", F_BOLD));

                primo = true;
                foreach (int cod in rel.AmbComercial.listCodAnalistas)
                {
                    if (primo)
                    {
                        p.Add("    \t\t" + cod + "\n");
                        primo = false;
                    }
                    else
                    {
                        p.Add("                                   \t" + cod + "\n");
                    }
                }
                oDoc.Add(p);

                DadosAnalises daAn = new DadosAnalises();
                List<Negocio.Analise.Consideracao> l_cons = daAn.getListConsideracao();
                string s_temp, s_ruid, s_lum;

                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nMedições:", F_BOLD));
                s_temp = "   " + ((Checklist)rel.AvalAnalista[0]).ValTemp + " ºC\n";
                s_ruid = "                    " + ((Checklist)rel.AvalAnalista[0]).ValRuido + " db(A)\n";
                s_lum = "                    " + ((Checklist)rel.AvalAnalista[0]).ValLum + " lm\n\n\n";
                p.Add(s_temp);
                p.Add(s_ruid);
                p.Add(s_lum);
                oDoc.Add(p);

                PdfPTable table = new PdfPTable(2);
                table.WidthPercentage = 70;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                PdfPCell cell;
                // Titulos
                cell = new PdfPCell(new Phrase("Consideração", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Cor", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                int aux;
                foreach (Negocio.Analise.Consideracao cons in l_cons)
                {
                    aux = rel.MapDescConsideracaoPrioridadePond[cons.Descricao];
                    cell = new PdfPCell(new Phrase(cons.Descricao, F_NORMAL));
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    table.AddCell(cell);
                    cell = new PdfPCell();
                    cell.BackgroundColor = getColor(aux);
                    table.AddCell(cell);
                }

                oDoc.Add(table);


                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nLegendas:", F_SMALLER_BOLD));
                p.Add(new Phrase("   Cor Vermelha -> Relevante\n", F_SMALLER_NORMAL));
                p.Add(new Phrase("                     Cor Amarela -> Significativo\n", F_SMALLER_NORMAL));
                p.Add(new Phrase("                     Cor Verde -> Trivial\n\n\n", F_SMALLER_NORMAL));
                oDoc.Add(p);

                string s_num;

                p = new Paragraph();
                table = new PdfPTable(3);
                p.Alignment = Element.ALIGN_CENTER;
                p.Add("Instrumentos de Observação");

                cell = new PdfPCell(new Phrase("Tipo", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Quantidade", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Data", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Questionário", F_SMALLER_BOLD));
                table.AddCell(cell);

                s_num = rel.AvalCliente.Count + "";
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                s_num = rel.AvalCliente[rel.AvalCliente.Count - 1].Doc_cabecalho.Data_registo.Day + "/" + rel.AvalCliente[rel.AvalCliente.Count - 1].Doc_cabecalho.Data_registo.Month + "/" + rel.AvalCliente[rel.AvalCliente.Count - 1].Doc_cabecalho.Data_registo.Year;
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Formulário Avaliação", F_SMALLER_BOLD));
                table.AddCell(cell);

                s_num = rel.AvalProfissional.Count + "";
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                s_num = rel.AvalProfissional[rel.AvalProfissional.Count - 1].Doc_cabecalho.Data_registo.Day + "/" + rel.AvalProfissional[rel.AvalProfissional.Count - 1].Doc_cabecalho.Data_registo.Month + "/" + rel.AvalProfissional[rel.AvalProfissional.Count - 1].Doc_cabecalho.Data_registo.Year;
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Checklist", F_SMALLER_BOLD));
                table.AddCell(cell);

                s_num = rel.AvalAnalista.Count + "";
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                s_num = rel.AvalAnalista[rel.AvalAnalista.Count - 1].Doc_cabecalho.Data_registo.Day + "/" + rel.AvalAnalista[rel.AvalAnalista.Count - 1].Doc_cabecalho.Data_registo.Month + "/" + rel.AvalAnalista[rel.AvalAnalista.Count - 1].Doc_cabecalho.Data_registo.Year;
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                oDoc.Add(table);

                p = new Paragraph();
                p.Alignment = Element.ALIGN_RIGHT;
                String s_final = "\n\nGerado a " + now.Day + "/" + now.Month + "/" + now.Year;
                p.Add(new Phrase(s_final, F_MINI_ITALIC));
                oDoc.Add(p);

                oDoc.Close();
            }
            catch (Exception e)
            {
                if (oDoc.IsOpen()) oDoc.CloseDocument();
                return 1;
            }

            try
            {

                FileInfo fInfo = new FileInfo(filename);
                if (fInfo.Length <= 1024) return 1;
                if (File.Exists(filename)) System.Diagnostics.Process.Start(filename);
            }
            catch (Exception e) { return 1; }

            return 0;
        }

        public int imprimirPrioridadeDimensao(RelatorioPrioridadesDimensao rel)
        {
            DateTime now = DateTime.Today;

            String filename = path + "Rel_PrioridadeDimensao_" + rel.AmbComercial.Cabeçalho.Nome + "_" + now.Day + now.Month + now.Year + ".pdf";
            Boolean primo;

            iTextSharp.text.Document oDoc = new iTextSharp.text.Document();

            try
            {

                PdfWriter writer = PdfWriter.GetInstance(oDoc, new FileStream(filename, FileMode.Create));

                oDoc.Open();

                PdfContentByte direct_c = writer.DirectContent;

                Paragraph p = new Paragraph();
                p.Alignment = Element.ALIGN_CENTER;
                p.Add(new Phrase("Relatório de Prioridades Por Dimensão\n", F_TITULO));
                string s_cod = "COD: " + rel.AmbComercial.Cabeçalho.Cod;
                p.Add(new Phrase(s_cod, F_SUBTITULO));
                oDoc.Add(p);

                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nCódigo Analistas:", F_BOLD));

                primo = true;
                foreach (int cod in rel.AmbComercial.listCodAnalistas)
                {
                    if (primo)
                    {
                        p.Add("    \t\t" + cod + "\n");
                        primo = false;
                    }
                    else
                    {
                        p.Add("                                   \t" + cod + "\n");
                    }
                }
                oDoc.Add(p);

                DadosAnalises daAn = new DadosAnalises();
                List<Negocio.Analise.Consideracao> l_cons = daAn.getListConsideracao();
                string s_temp, s_ruid, s_lum;

                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nMedições:", F_BOLD));
                s_temp = "   " + ((Checklist)rel.AvalAnalista[0]).ValTemp + " ºC\n";
                s_ruid = "                    " + ((Checklist)rel.AvalAnalista[0]).ValRuido + " db(A)\n";
                s_lum = "                    " + ((Checklist)rel.AvalAnalista[0]).ValLum + " lm\n\n\n";
                p.Add(s_temp);
                p.Add(s_ruid);
                p.Add(s_lum);
                oDoc.Add(p);

                PdfPTable table = new PdfPTable(4);
                table.WidthPercentage = 100;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                PdfPCell cell;
                // Titulos
                cell = new PdfPCell(new Phrase("\nConsideração", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.PaddingBottom += 10;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_BOTTOM;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Cliente", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Profissional", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Analista", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                List<int> aux;
                foreach (Negocio.Analise.Consideracao cons in l_cons)
                {
                    aux = rel.MapDescConsideracaoPrioridades[cons.Descricao];
                    cell = new PdfPCell(new Phrase(cons.Descricao, F_NORMAL));
                    cell.SetLeading(20f, 0f);
                    table.AddCell(cons.Descricao);
                    cell = new PdfPCell();
                    cell.BackgroundColor = getColor(aux[0]);
                    table.AddCell(cell);
                    cell = new PdfPCell();
                    cell.BackgroundColor = getColor(aux[1]);
                    table.AddCell(cell);
                    cell = new PdfPCell();
                    cell.BackgroundColor = getColor(aux[2]);
                    table.AddCell(cell);
                }

                oDoc.Add(table);


                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nLegendas:", F_SMALLER_BOLD));
                p.Add(new Phrase("   Cor Vermelha -> Relevante\n", F_SMALLER_NORMAL));
                p.Add(new Phrase("                     Cor Amarela -> Significativo\n", F_SMALLER_NORMAL));
                p.Add(new Phrase("                     Cor Verde -> Trivial\n", F_SMALLER_NORMAL));
                p.Add(new Phrase("                     Cor Cinzenta -> Não Avaliado\n\n\n", F_SMALLER_NORMAL));
                oDoc.Add(p);

                string s_num;

                p = new Paragraph();
                table = new PdfPTable(3);
                p.Alignment = Element.ALIGN_CENTER;
                p.Add("Instrumentos de Observação");

                cell = new PdfPCell(new Phrase("\nTipo", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.PaddingBottom += 10;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_BOTTOM;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Quantidade", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Data", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Questionário", F_SMALLER_BOLD));
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                s_num = rel.AvalCliente.Count + "";
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                s_num = rel.AvalCliente[rel.AvalCliente.Count - 1].Doc_cabecalho.Data_registo.Day + "/" + rel.AvalCliente[rel.AvalCliente.Count - 1].Doc_cabecalho.Data_registo.Month + "/" + rel.AvalCliente[rel.AvalCliente.Count - 1].Doc_cabecalho.Data_registo.Year;
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Formulário Avaliação", F_SMALLER_BOLD));
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                s_num = rel.AvalProfissional.Count + "";
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                s_num = rel.AvalProfissional[rel.AvalProfissional.Count - 1].Doc_cabecalho.Data_registo.Day + "/" + rel.AvalProfissional[rel.AvalProfissional.Count - 1].Doc_cabecalho.Data_registo.Month + "/" + rel.AvalProfissional[rel.AvalProfissional.Count - 1].Doc_cabecalho.Data_registo.Year;
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("Checklist", F_SMALLER_BOLD));
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                s_num = rel.AvalAnalista.Count + "";
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                s_num = rel.AvalAnalista[rel.AvalAnalista.Count - 1].Doc_cabecalho.Data_registo.Day + "/" + rel.AvalAnalista[rel.AvalAnalista.Count - 1].Doc_cabecalho.Data_registo.Month + "/" + rel.AvalAnalista[rel.AvalAnalista.Count - 1].Doc_cabecalho.Data_registo.Year;
                cell = new PdfPCell(new Phrase(s_num, F_SMALLER_NORMAL));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                oDoc.Add(table);

                p = new Paragraph();
                p.Alignment = Element.ALIGN_RIGHT;
                String s_final = "\n\nGerado a " + now.Day + "/" + now.Month + "/" + now.Year;
                p.Add(new Phrase(s_final, F_MINI_ITALIC));
                oDoc.Add(p);

                oDoc.Close();
            }
            catch (Exception e)
            {
                if (oDoc.IsOpen()) oDoc.CloseDocument();
                return 1;
            }

            try
            {

                FileInfo fInfo = new FileInfo(filename);
                if (fInfo.Length <= 1024) return 1;
                if (File.Exists(filename)) System.Diagnostics.Process.Start(filename);
            }
            catch (Exception e) { return 1; }

            return 0;
        }

        public int imprimirProfissional(RelatorioProfissional rel)
        {

            DateTime now = DateTime.Today;

            String filename = path + "Rel_Profissional_" + rel.AmbComercial.Cabeçalho.Nome + "_" + now.Day + now.Month + now.Year + ".pdf";
            Boolean primo;

            iTextSharp.text.Document oDoc = new iTextSharp.text.Document();
            try
            {
                PdfWriter writer = PdfWriter.GetInstance(oDoc, new FileStream(filename, FileMode.Create));

                oDoc.Open();

                PdfContentByte direct_c = writer.DirectContent;

                Paragraph p = new Paragraph();
                p.Alignment = Element.ALIGN_CENTER;
                p.Add(new Phrase("Relatório Profissional\n", F_TITULO));
                string s_cod = "COD: " + rel.AmbComercial.Cabeçalho.Cod;
                p.Add(new Phrase(s_cod, F_SUBTITULO));
                oDoc.Add(p);

                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nCódigo Analistas:", F_BOLD));

                primo = true;
                foreach (int cod in rel.AmbComercial.listCodAnalistas)
                {
                    if (primo)
                    {
                        p.Add("    \t\t" + cod + "\n");
                        primo = false;
                    }
                    else
                    {
                        p.Add("                                   \t" + cod + "\n");
                    }
                }
                oDoc.Add(p);

                DadosAnalises daAn = new DadosAnalises();
                List<Negocio.Analise.Consideracao> l_cons = daAn.getListConsideracao();

                List<Negocio.AmbienteComercial.Divisao> l_div = rel.AmbComercial.listDivisao;
                List<int> resAval;
                string s_neg, s_sat, s_pos, s_divisao;
                int cod_div_bd;
                int i;


                p = new Paragraph();
                p.Add(new Phrase("\nGeral:\n\n", F_BOLD));
                oDoc.Add(p);

                PdfPTable table = new PdfPTable(5);
                table.WidthPercentage = 100;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                PdfPCell cell;
                // Titulos
                cell = new PdfPCell(new Phrase("\nConsideração", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.PaddingBottom += 10;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_BOTTOM;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Negativo", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Satisfatório", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Positivo", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Pior Zona", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                table.AddCell(cell);

                foreach (Negocio.Analise.Consideracao cons in l_cons)
                {
                    resAval = rel.getDivisaoConsPercent(-1, cons.Cod);
                    cell = new PdfPCell(new Phrase(cons.Descricao, F_NORMAL));
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(cell);
                    s_neg = resAval[0] + "%";
                    cell = new PdfPCell(new Phrase(s_neg, F_NORMAL));
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(cell);
                    s_sat = resAval[1] + "%";
                    cell = new PdfPCell(new Phrase(s_sat, F_NORMAL));
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(cell);
                    s_pos = resAval[2] + "%";
                    cell = new PdfPCell(new Phrase(s_pos, F_NORMAL));
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(cell);
                    cod_div_bd = rel.getPiorDivisao(cons.Cod);
                    for (i = 0; l_div[i].CodBD != cod_div_bd; i++) ;

                    cell = new PdfPCell(new Phrase(l_div[i].Descricao, F_NORMAL));
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(cell);
                }

                oDoc.Add(table);


                foreach (Negocio.AmbienteComercial.Divisao div in l_div)
                {
                    if (rel.MapDivUsada.ContainsKey(div.CodRelativo))
                    {
                        p = new Paragraph();
                        s_divisao = "\n\n" + div.Descricao + ":\n\n";
                        p.Add(new Phrase(s_divisao, F_BOLD));
                        oDoc.Add(p);

                        table = new PdfPTable(4);
                        table.WidthPercentage = 100;
                        table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                        // Titulos
                        cell = new PdfPCell(new Phrase("\nConsideração", F_SMALLER_BOLD));
                        cell.GrayFill = 0.8F;
                        cell.PaddingBottom += 10;
                        cell.HorizontalAlignment = Element.ALIGN_CENTER;
                        cell.VerticalAlignment = Element.ALIGN_BOTTOM;
                        table.AddCell(cell);
                        cell = new PdfPCell(new Phrase("Negativo", F_SMALLER_BOLD));
                        cell.GrayFill = 0.8F;
                        cell.HorizontalAlignment = Element.ALIGN_CENTER;
                        cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                        table.AddCell(cell);
                        cell = new PdfPCell(new Phrase("Satisfatório", F_SMALLER_BOLD));
                        cell.GrayFill = 0.8F;
                        cell.HorizontalAlignment = Element.ALIGN_CENTER;
                        cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                        table.AddCell(cell);
                        cell = new PdfPCell(new Phrase("Positivo", F_SMALLER_BOLD));
                        cell.GrayFill = 0.8F;
                        cell.HorizontalAlignment = Element.ALIGN_CENTER;
                        cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                        table.AddCell(cell);

                        foreach (Negocio.Analise.Consideracao cons in l_cons)
                        {
                            resAval = rel.getDivisaoConsPercent(div.CodBD, cons.Cod);
                            cell = new PdfPCell(new Phrase(cons.Descricao, F_NORMAL));
                            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                            table.AddCell(cell);
                            s_neg = resAval[0] + "%";
                            cell = new PdfPCell(new Phrase(s_neg, F_NORMAL));
                            cell.HorizontalAlignment = Element.ALIGN_CENTER;
                            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                            table.AddCell(cell);
                            s_sat = resAval[1] + "%";
                            cell = new PdfPCell(new Phrase(s_sat, F_NORMAL));
                            cell.HorizontalAlignment = Element.ALIGN_CENTER;
                            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                            table.AddCell(cell);
                            s_pos = resAval[2] + "%";
                            cell = new PdfPCell(new Phrase(s_pos, F_NORMAL));
                            cell.HorizontalAlignment = Element.ALIGN_CENTER;
                            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                            table.AddCell(cell);
                        }
                        oDoc.Add(table);
                    }
                    else
                    {
                        p = new Paragraph();
                        p = new Paragraph();
                        s_divisao = "\n\n" + div.Descricao + ":  Não possui nenhuma informação sobre esta divisão";
                        p.Add(new Phrase(s_divisao, F_BOLD));
                        oDoc.Add(p);
                    }
                }


                p = new Paragraph();
                p.Alignment = Element.ALIGN_RIGHT;
                String s_final = "Gerado a " + now.Day + "/" + now.Month + "/" + now.Year;
                p.Add(new Phrase(s_final, F_MINI_ITALIC));
                oDoc.Add(p);

                oDoc.Close();


            }
            catch (Exception e)
            {
                if (oDoc.IsOpen()) oDoc.CloseDocument();
                return 1;
            }

            try
            {
                FileInfo fInfo = new FileInfo(  filename);
                if (fInfo.Length <= 1024) return 1;
                if (File.Exists(  filename)) System.Diagnostics.Process.Start(  filename);
            }
            catch (Exception e) { return 1; }

            

            return 0;

        }

        public int imprimirCliente(RelatorioCliente res)
        {
            DateTime now = DateTime.Today;
            Boolean primo;

            String filename = path + "Rel_Cliente_" + res.AmbComercial.Cabeçalho.Nome + "_" + now.Day + now.Month + now.Year + ".pdf";
            iTextSharp.text.Document oDoc = new iTextSharp.text.Document();

            try
            {
                PdfWriter writer = PdfWriter.GetInstance(oDoc, new FileStream(filename, FileMode.Create));
                oDoc.Open();


                Paragraph p = new Paragraph();
                p.Alignment = Element.ALIGN_CENTER;
                p.Add(new Phrase("Relatório Cliente\n", F_TITULO));
                string cod_amb = "COD: " + res.AmbComercial.Cabeçalho.Cod;
                p.Add(new Phrase(cod_amb, F_SUBTITULO));
                oDoc.Add(p);


                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nCódigo Analistas:", F_BOLD));

                primo = true;
                foreach (int cod in res.AmbComercial.listCodAnalistas)
                {
                    if (primo)
                    {
                        p.Add("    \t\t" + cod + "\n");
                        primo = false;
                    }
                    else
                    {
                        p.Add("                                   \t" + cod + "\n");
                    }
                }
                oDoc.Add(p);


                p = new Paragraph();
                p.Font = F_NORMAL;
                p.Alignment = Element.ALIGN_LEFT;
                p.Add(new Phrase("\nGénero:", F_BOLD));
                int percFem = (int)(((float)res.NumFeminino / (float)(res.NumFeminino + res.NumMasculino)) * 100);
                int percMasc = 100 - percFem;
                string s_perc_fem = percFem + "%";
                string s_perc_masc = percMasc + "%";
                p.Add("   " + s_perc_masc + " Masculino\n");
                p.Add("                 " + s_perc_fem + " Feminino\n");
                oDoc.Add(p);


                List<int> x = new List<int>();
                x.Add(5); x.Add(10); x.Add(15); x.Add(20); x.Add(25); x.Add(30); x.Add(35); x.Add(40);
                x.Add(45); x.Add(50); x.Add(55); x.Add(60); x.Add(65);


                List<int> y = new List<int>(13);
                for (int aux_i = 0; aux_i < 13; aux_i++) y.Add(0);
                float mean = criaPontosIdades(ref y, res.ListaIdades);
                int max = res.ListaIdades.Max();
                int min = res.ListaIdades.Min();

                p = new Paragraph();
                p.Add(new Phrase("\nIdades:\n", F_BOLD));

                float n_mean = (float)Decimal.Round((decimal)mean, 2, MidpointRounding.AwayFromZero);
                string s_media = "     Media: " + n_mean + "\n";
                p.Add(new Phrase(s_media, F_SMALLER_NORMAL));
                string s_intervalo = "      Intervalo: [" + min + "," + max + "]\n\n";
                p.Add(new Phrase(s_intervalo, F_SMALLER_NORMAL));


                new Modelo_ETdA.Interface.chart(x, y);
                String img_path = Directory.GetCurrentDirectory() + "/dat.jpg";
                Image im = Image.GetInstance(img_path);
                im.ScalePercent(50f);
                p.Add(im);
                oDoc.Add(p);

                DadosDocumentos dado = new DadosDocumentos();

                int sum_habil = 0;
                float sum_habil_rel = 0;
                foreach (KeyValuePair<int, int> par in res.MapHabilQtd) sum_habil += par.Value;


                p = new Paragraph();
                p.Add(new Phrase("Habilitações:\n\n", F_BOLD));
                oDoc.Add(p);

                PdfPTable table = new PdfPTable(3);
                table.WidthPercentage = 100;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                PdfPCell cell;
                // Titulos
                cell = new PdfPCell(new Phrase("Habilitação", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Quantidade Absoluta", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Quantidade Relativa", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                List<Negocio.Analise.Avaliacao.Habilitacao> l_hab = dado.getListHabilitacaoLiteraria();
                int i = 0;
                string s_qtd_abs;
                string s_qtd_rel;
                foreach (KeyValuePair<int, int> par in res.MapHabilQtd)
                {
                    table.AddCell(l_hab[par.Key - 1].descricao);
                    s_qtd_abs = "" + par.Value;
                    table.AddCell(s_qtd_abs);
                    if (i == (l_hab.Count - 1)) s_qtd_rel = (Math.Round((((float)(1 - sum_habil_rel))) * 100)) + "%";
                    else s_qtd_rel = ((int)(((float)par.Value / (float)sum_habil) * 100)) + "%";
                    table.AddCell(s_qtd_rel);
                    sum_habil_rel += ((float)par.Value / (float)sum_habil);
                    i++;
                }
                oDoc.Add(table);


                p = new Paragraph();
                p.Add(new Phrase("\n\nRespostas:\n\n", F_BOLD));
                oDoc.Add(p);

                table = new PdfPTable(4);
                table.WidthPercentage = 100;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                // Titulos
                cell = new PdfPCell(new Phrase("Consideração", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Negativo", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Satisfatório", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Positivo", F_SMALLER_BOLD));
                cell.GrayFill = 0.8F;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);

                List<DistribuicaoRespostas> l_dist_resp = res.ListDistRespostas;
                DadosAnalises daAn = new DadosAnalises();
                List<Negocio.Analise.Consideracao> l_cons = daAn.getListConsideracao();

                i = 0;
                int sum_resp_abs = 0;
                int sum_resp_rel = 0;
                string resp_neg, resp_sat, resp_pos;
                foreach (DistribuicaoRespostas d_resp in l_dist_resp)
                {
                    sum_resp_abs = d_resp.NumRespNeg + d_resp.NumRespPos + d_resp.NumRespSatisf;
                    table.AddCell(l_cons[(d_resp.CodConsideracao - 1)].Descricao);
                    resp_neg = ((int)((((float)d_resp.NumRespNeg / (float)sum_resp_abs)) * 100)) + "%";
                    sum_resp_rel += ((int)((((float)d_resp.NumRespNeg / (float)sum_resp_abs)) * 100));
                    table.AddCell(resp_neg);
                    resp_sat = ((int)((((float)d_resp.NumRespSatisf / (float)sum_resp_abs)) * 100)) + "%";
                    sum_resp_rel += ((int)((((float)d_resp.NumRespSatisf / (float)sum_resp_abs)) * 100));
                    table.AddCell(resp_sat);
                    resp_pos = (100 - sum_resp_rel) + "%";
                    table.AddCell(resp_pos);

                    sum_resp_rel = 0;
                    sum_resp_abs = 0;
                    i++;
                }
                oDoc.Add(table);

                p = new Paragraph();
                p.Alignment = Element.ALIGN_RIGHT;
                String s_final = "Gerado a " + now.Day + "/" + now.Month + "/" + now.Year;
                p.Add(new Phrase(s_final, F_MINI_ITALIC));
                oDoc.Add(p);


                oDoc.Close();
            }
            catch (Exception e)
            {
                if (oDoc.IsOpen()) oDoc.CloseDocument();
                if (File.Exists(Directory.GetCurrentDirectory() + "/dat.jpg")) File.Delete(Directory.GetCurrentDirectory() + "/dat.jpg");
                return 1;
            }

            try
            {
                if (File.Exists(Directory.GetCurrentDirectory() + "/dat.jpg")) File.Delete(Directory.GetCurrentDirectory() + "/dat.jpg");

                FileInfo fInfo = new FileInfo(filename);
                if (fInfo.Length <= 1024) return 1;
                if (File.Exists(filename)) System.Diagnostics.Process.Start(filename);

            }
            catch (Exception e) { return 1; }

            return 0;
        }


    }
}
